###############
# Replication Code for: The Electoral Consequences of International Migration in Sending Countries
# Appendix
###############

#Working directory
argv <- getwd()
setwd(argv)

# Load libraries 
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org")
pacman::p_load(stargazer,tidyverse,tidyr,plm, sandwich, lmtest, psych, broom, purr, magrittr, xtable)


#########
#A.1. Descriptive Statistics (LiTs 2010)

lits<- read.csv("lits_2_selected.csv", stringsAsFactors = F)

lits<- lits %>% mutate(BA = ifelse(educ >=6, 1, 0))

a1_sumstat<- describe(lits %>% select(emig, age, Female, educ, BA, anti_imm, support_dem, rlg_mem, unemployed, vote, sat_eco)) %>% as.data.frame()

a1_sumstat<- a1_sumstat %>% mutate(Variable=row.names(a1_sumstat)) %>%
  select(Variable, n, mean, min, max, sd) %>% mutate_if(is.numeric, round, 2) %>% 
  mutate(n = round(n, digits = 0)) %>%
  set_rownames(c("Willing to Emigrate", "Age", "Female","Education(EISCED)", "Education (BA or higher)" ,"Anti-Immigrant", "Pro-Democracy", "Religiosity", "Unemployed", "Vote", "Satisfied w Econ")) %>% dplyr::select(-Variable)

print(xtable(a1_sumstat), include.rownames = T) #Table. A.1

#######################################################################
#A.2. Descriptive Statistics (ESS Wave 5-9)

ess<- read.csv("ess_5_9_selected.csv", stringsAsFactors = F)

ess<- ess %>% mutate(BA = ifelse(eisced >=6, 1, 0))

a2_sumstat<- describe(ess %>% select(forborn, agem_correct, female, eisced, BA,  anti_race_imm, lrscale, rlgdgr)) %>% as.data.frame() 

a2_sumstat<- a2_sumstat %>%mutate(Variable=row.names(a2_sumstat)) %>% 
  select(Variable, n, mean, min, max, sd) %>% mutate_if(is.numeric, round, 2) %>% 
  set_rownames(c("Emigrants", "Age (of emigration)", "Female", "Education (EISCED)", "Education (BA or Higher)" ,"Anti-Immigrant", "Ideology", "Religiosity")) %>% dplyr::select(-Variable)

print(xtable(a2_sumstat), include.rownames = T) #Table. A.2

#######################################################################
#A.3. T-Test: (Potential) Emigrants vs. Stayers (LiTs)

a3_t_test_tab<- lapply(c(lits["age"], lits["Female"], 
     lits['educ'], lits['BA'], lits['unemployed'], lits['sat_eco'], lits['vote'],
    lits['support_dem'], lits['anti_imm'], lits['rlg_mem']), 
    function(i) t.test(i ~ lits$emig))

a3_t_test_tab<- sapply(c(1:10), function(x) broom::tidy(a3_t_test_tab[[x]])) %>% 
  t() %>% as.data.frame() %>%
  dplyr::select(estimate2, estimate1, estimate, conf.low, conf.high, p.value) %>% magrittr::set_colnames(
    c("Emigrants", "Natives", "Difference", "CI_low", "CI_high", "p-value")) %>%
  mutate_if(is.numeric, round, 3)

Variable<- c("Age", "Female", "Education", "Education (BA or Higher)" ,"Unemployed", "Satisfied with Econ", "Vote", "Support for Democracy", "Anti-Immigrant", "Religiosity 1")

print(xtable(cbind(Variable, a3_t_test_tab), include.rownames = F)) #A.3. 

#######################################################################
#A.4. T-Test: Emigrants vs. Stayers (ESS)

a4_t_test_tab<-lapply(c(ess["agem_correct"], ess["female"], ess['eisced'], ess['BA'],
                        ess['lrscale'], ess['anti_race_imm'], ess['rlgdgr']), 
                      function(i) t.test(i ~ ess$forborn))

a4_t_test_tab<- sapply(c(1:7), function(x) broom::tidy(a4_t_test_tab[[x]])) %>% 
  t() %>% as.data.frame() %>%
  dplyr::select(estimate2, estimate1, estimate, conf.low, conf.high) %>%
  magrittr::set_colnames(
    c("Emigrants", "Natives", "Difference", "CI_low", "CI_high")) %>%
  mutate_if(is.numeric, round, 3)

Variable<- c("Age (of arrival)", "Female", "Education", "Education (BA or Higher)" ,"Idegology", "Anti-Immigrant",  "Religiosity")

print(xtable(cbind(Variable, a4_t_test_tab), include.rownames = F)) #A.4

#######################################################################
#A.6. Vote Share of Far-right Parties with Proxy for Remittances

ches_emig_dat<- read.csv("ches_emig_dat.csv", stringsAsFactors = F)

a6_m1 <- lm(data = ches_emig_dat %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+log(Pop)+dis_inc.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a6_m1_se<- coeftest(a6_m1, vcov=vcovHC(a6_m1, type="HC0"))

a6_m2 <- lm(data = ches_emig_dat %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+log(Pop)+dis_inc.lag.1+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year))
a6_m2_se<- coeftest(a6_m2, vcov=vcovHC(a6_m2, type="HC0"))

a6_m3 <- lm(data = ches_emig_dat %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+log(Pop)+dis_inc.lag.1+WB_rem.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a6_m3_se<- coeftest(a6_m3, vcov=vcovHC(a6_m3, type="HC0"))

a6_m4 <- lm(data = ches_emig_dat %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+log(Pop)+unemp.percent.lag.1.n2+dis_inc.lag.1+WB_rem.lag.1+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year))
a6_m4_se<- coeftest(a6_m4, vcov=vcovHC(a6_m4, type="HC0"))

#Table A.6
stargazer::stargazer(a6_m1, a6_m2, a6_m3, a6_m4, dep.var.caption = "Dependent Variable: Vote Share of Far-right Parties",dep.var.labels.include = F,
  omit=c("nuts_code2013","Year","Constant", "voteshare.w1"), 
  se = list(a6_m1_se[,2], a6_m2_se[,2], a6_m3_se[,2], a6_m4_se[,2]),align=TRUE, 
  omit.stat=c("f", "ser","adj.rsq", "rsq"), 
  add.lines = list(c("NUTS FE", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"), 
  c("Year FE", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV", "", "\\checkmark","" ,"\\checkmark")), 
  order = c("emig.lag.1.voters", "imig.lag.1.voters", "gdp.lag.1", "unemp.percent.lag.1.n2", 
  'Pop', "dis_inc.lag.1", "WB_rem.lag.1"),
  covariate.labels = c("Emigration", "Immigration","GDP", "Unemployment","Population",
  "Disposable Income","(National) Remittances"))

#######################################################################
#Table A.7.Emigration and Vote Shares of Other Parties

#Left Party (Table A.7 M1-M3)
a7_l1 <- lm(data = ches_emig_dat %>% filter(CHES == "rad left"), voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year))
a7_l1_se<- coeftest(a7_l1, vcov=vcovHC(a7_l1, type="HC0"))

a7_l2 <- lm(data = ches_emig_dat %>% filter(CHES == "rad left"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+as.factor(nuts_code2013)+as.factor(Year))
a7_l2_se<- coeftest(a7_l2, vcov=vcovHC(a7_l2, type="HC0"))

a7_l3 <- lm(data = ches_emig_dat %>% filter(CHES == "rad left"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a7_l3_se<- coeftest(a7_l3, vcov=vcovHC(a7_l3, type="HC0"))

#Socialist Party (Table A.7 M4-M6)
a7_sc1 <- lm(data = ches_emig_dat %>% filter(CHES == "socialist"), voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year))
a7_sc1_se<- coeftest(a7_sc1, vcov=vcovHC(a7_sc1, type="HC0"))

a7_sc2 <- lm(data = ches_emig_dat %>% filter(CHES == "socialist"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+as.factor(nuts_code2013)+as.factor(Year))
a7_sc2_se<- coeftest(a7_sc2, vcov=vcovHC(a7_sc2, type="HC0"))

a7_sc3 <- lm(data = ches_emig_dat %>% filter(CHES == "socialist"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a7_sc3_se<- coeftest(a7_sc3, vcov=vcovHC(a7_sc3, type="HC0"))

#Centrist (Liberal) Party (Table A.7 M7-M99)
a7_lib1 <- lm(data = ches_emig_dat %>% filter(CHES == "liberal"), voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year))
a7_lib1_se<- coeftest(a7_lib1, vcov=vcovHC(a7_lib1, type="HC0"))

a7_lib2 <- lm(data = ches_emig_dat %>% filter(CHES == "liberal"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+as.factor(nuts_code2013)+as.factor(Year))
a7_lib2_se<- coeftest(a7_lib2, vcov=vcovHC(a7_lib2, type="HC0"))

a7_lib3 <- lm(data = ches_emig_dat %>% filter(CHES == "liberal"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a7_lib3_se<- coeftest(a7_lib3, vcov=vcovHC(a7_lib3, type="HC0"))

stargazer::stargazer(
  a7_l1, a7_l2, a7_l3, a7_sc1, a7_sc2, a7_sc3, a7_lib1, a7_lib2, a7_lib3,
  se = list(a7_l1_se[,2], a7_l2_se[,2], a7_l3_se[,2], 
            a7_sc1_se[,2], a7_sc2_se[,2], a7_sc3_se[,3], 
            a7_lib1_se[,2], a7_lib2_se[,2], a7_lib3_se[,2]), 
  omit=c("nuts_code2013","Year","Constant", "voteshare.w1"), 
  align=TRUE, omit.stat=c("f", "ser","adj.rsq", "rsq"), 
  covariate.labels = c("Emigration", "Immigration", "GDP",  "Unemployment", "Current Transfers"),
  add.lines = list(c("NUTS FE", "\\checkmark", "\\checkmark", "\\checkmark","\\checkmark", "\\checkmark", "\\checkmark","\\checkmark", "\\checkmark", "\\checkmark"), c("Year FE", "\\checkmark", "\\checkmark", "\\checkmark","\\checkmark", "\\checkmark", "\\checkmark","\\checkmark", "\\checkmark", "\\checkmark")))


#######################################################################
#######################################################################
#Table A.8

turnout<- ches_emig_dat %>% dplyr::select(turnout, turnout.w1, emig.lag.1.voters, nuts_code2013, Year, imig.lag.1.voters, unemp.percent.lag.1.n2, Pop.lag.1, Pop, gdp.lag.1, unemp.percent.lag.1.n2, c_tf_r.lag.1, WB_rem.lag.1, dis_inc.lag.1) %>% unique()

a8_t0 <- lm(data = turnout, turnout ~  emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year))
a8_t0_se<- coeftest(a8_t0, vcov=vcovHC(a8_t0, type="HC0"))

a8_t1 <- lm(data = turnout, turnout ~  emig.lag.1.voters+imig.lag.1.voters+ gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+turnout.w1+as.factor(nuts_code2013)+as.factor(Year))
a8_t1_se<- coeftest(a8_t1, vcov=vcovHC(a8_t1, type="HC0"))

a8_t2 <- lm(data = turnout, turnout ~  emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+WB_rem.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year))
a8_t2_se<- coeftest(a8_t2, vcov=vcovHC(a8_t2, type="HC0"))

a8_t3 <- lm(data = turnout, turnout ~  emig.lag.1.voters+imig.lag.1.voters+ gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+WB_rem.lag.1+log(Pop)+turnout.w1+as.factor(nuts_code2013)+as.factor(Year))
a8_t3_se<- coeftest(a8_t3, vcov=vcovHC(a8_t3, type="HC0"))

stargazer::stargazer(a8_t0, a8_t1, a8_t2, a8_t3,
omit=c("nuts_code2013", "Year", "Constant", "turnout.w1"),
omit.stat = c("ser", "f", "rsq", "adj.rsq"), 
se = list(a8_t0_se[,2], a8_t1_se[,2], a8_t2_se[,2], a8_t3_se[,2]), 
order = c("emig.lag.1", "imig.lag.1", "gdp.lag.1", "unemp.percent.lag.1.n2", "c_tf_r.lag.1", "Pop", "WB_rem.lag.1"),
covariate.labels = c("Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Population", "(National) Remittance"),add.lines = list(
c("NUTS FE", "\\checkmark",  "\\checkmark","\\checkmark",  "\\checkmark"),
c("Year FE","\\checkmark",  "\\checkmark", "\\checkmark",  "\\checkmark"), 
c("Lagged DV",  "","\\checkmark", "","\\checkmark")))

#######################################################################
#Table A.9. Emigration and Vote Share of Far-right Parties (with alternative coding)

ches_emig_alt<- read.csv("ches_emig_dat_alternative.csv", stringsAsFactors = F)

a9_m1 <- lm(data = ches_emig_alt %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year))
a9_m1_se<- coeftest(a9_m1, vcov=vcovHC(a9_m1, type="HC0"))

a9_m2 <- lm(data = ches_emig_alt %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a9_m2_se<- coeftest(a9_m2, vcov=vcovHC(a9_m2, type="HC0"))

a9_m3 <- lm(data = ches_emig_alt %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+WB_rem.lag.1+as.factor(nuts_code2013)+as.factor(Year))
a9_m3_se<- coeftest(a9_m3, vcov=vcovHC(a9_m3, type="HC0"))

a9_m4 <- lm(data = ches_emig_alt %>% filter(CHES == "rad right"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+WB_rem.lag.1+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year))
a9_m4_se<- coeftest(a9_m4, vcov=vcovHC(a9_m4, type="HC0"))

#Table A.9
stargazer::stargazer(a9_m1, a9_m2, a9_m3, a9_m4, 
  omit=c('nuts_code2013', 'Year', 'Constant', "voteshare.w1"), omit.stat = c('rsq', 'adj.rsq', 'ser', 'f'), 
  se = list(a9_m1_se[,2], a9_m2_se[,2], a9_m3_se[,2], a9_m4_se[,2]),
  add.lines=list(c("NUTS3 FE", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),
  c("Year FE", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV", "", "", "", "\\checkmark")), covariate.labels = c("Emigration", "Immigration","GDP", "Unemployment", "Current Transfers", "(National) Remittances"))

#######################################################################
#Table A.10 IV Analysis (Poland and Romania)

pl_ro_sample<- read.csv("ches_emig_dat_pl_ro.csv", stringsAsFactors = F)

a10_lm1 <- lm(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year))
a10_lm1_se<- coeftest(a10_lm1, vcov=vcovHC(a10_lm1, type="HC0"))

a10_iv1 <- AER::ivreg(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+as.factor(nuts_code2013)+(unemp*emig.share.t1))
a10_iv1_diag<- summary(a10_iv1, diagnostics = T)
a10_iv1_se<- coeftest(a10_iv1, vcov=vcovHC(a10_iv1, type="HC0"))

a10_lm2 <- lm(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year))
a10_lm2_se<- coeftest(a10_lm2, vcov=vcovHC(a10_lm2, type="HC0"))

a10_iv2 <- AER::ivreg(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+(unemp*emig.share.t1))
a10_iv2_diag<- summary(a10_iv2, diagnostics = T)
a10_iv2_se<- coeftest(a10_iv2, vcov=vcovHC(a10_iv2, type="HC0"))

a10_lm3 <- lm(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year))
a10_lm3_se<- coeftest(a10_lm3, vcov=vcovHC(a10_lm3, type="HC0"))

a10_iv3 <- AER::ivreg(data=pl_ro_sample, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+(unemp*emig.share.t1))
a10_iv3_diag <- summary(a10_iv3, diagnostics = T)
a10_iv3_se<- coeftest(a10_iv3, vcov=vcovHC(a10_iv3, type="HC0"))

#Table A10
stargazer::stargazer(
  a10_lm1, a10_iv1_se, a10_lm2, a10_iv2_se, a10_lm3, a10_iv3_se, se=list(a10_lm1_se[,2], a10_iv1_se[,2], a10_lm2_se[,2], a10_iv2_se[,2], a10_lm3_se[,2], a10_iv3_se[,2]),
  omit = c('nuts_code2013', 'Year', 'Constant', 'voteshare.w1'), 
  omit.stat = c('ser', 'adj.rsq','rsq', 'f'), 
  dep.var.labels.include = F, model.names = F, model.numbers = F,
  column.labels = c("OLS", "IV", "OLS", "IV","OLS", "IV"), 
  covariate.labels = c("Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Population"), add.lines = list(c("NUTS3 FE","\\checkmark", "\\checkmark","\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),c("Year FE","\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV","", "", "", "", "\\checkmark", "\\checkmark"), c("First Stage F", "",  a10_iv1_diag$diagnostics[1,3], "", a10_iv2_diag$diagnostics[1,3], "", a10_iv3_diag$diagnostics[1,3])))

#######################################################################
#Table A.11. IV Analysis (Poland w Alternative Coding)

#dependent variable with alternative coding for party family
pl_sample2<- left_join(ches_emig_alt %>% filter(stringr::str_sub(nuts_code2013, 1, 2) == "PL") %>% dplyr::select(Year, nuts_code2013, voteshare, voteshare.w1), pl_ro_sample %>% filter(stringr::str_sub(nuts_code2013, 1, 2) == "PL") %>% dplyr::select(-c(voteshare, voteshare.w1)))

a11_iv0 <- AER::ivreg(data=pl_sample2, voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year) |. - emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year)+(unemp*emig.share.t1))
a11_iv0_se<- coeftest(a11_iv0, vcov=vcovHC(a11_iv0, type="HC0"))
a11_iv0_diag <- summary(a11_iv0, diagnostics = T)

a11_iv1 <- AER::ivreg(data=pl_sample2, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year)+gdp.lag.1+c_tf_r.lag.1+unemp.percent.lag.1.n2+(unemp*emig.share.t1))
a11_iv1_se<- coeftest(a11_iv1, vcov=vcovHC(a11_iv1, type="HC0"))
a11_iv1_diag <- summary(a11_iv1, diagnostics = T)

a11_iv2 <- AER::ivreg(data=pl_sample2, voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+c_tf_r.lag.1+as.factor(nuts_code2013)+as.factor(Year)+gdp.lag.1+unemp.percent.lag.1.n2+voteshare.w1+(unemp*emig.share.t1))
a11_iv2_se<- coeftest(a11_iv2, vcov=vcovHC(a11_iv2, type="HC0"))
a11_iv2_diag <- summary(a11_iv2, diagnostics = T)

#add a table code here. 

stargazer::stargazer(a11_iv0_se, a11_iv1_se, a11_iv2_se, 
  omit = c('nuts_code2013', 'Year', 'Constant', 'voteshare.w1'), 
  omit.stat = c('ser', 'adj.rsq','rsq', 'f'), dep.var.labels.include = F, model.names = F,
  covariate.labels = c("Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Population"),add.lines = list(c("Observations", a11_iv0$n, a11_iv1$n, a11_iv2$n),
    c("NUTS3 FE","\\checkmark", "\\checkmark","\\checkmark"),c("Year FE","\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV","", "", "\\checkmark"), c("First Stage F", round(a11_iv0_diag$diagnostics[1,3], 3), round(a11_iv1_diag$diagnostics[1,3],3), round(a11_iv2_diag$diagnostics[1,3],3))))


######################################################################
#Table A.12. IV analysis with 2004 emigration rates (Poland)

a12_iv1 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+as.factor(nuts_code2013)+(unemp*emig.share.t0))
a12_iv1_diag<- summary(a12_iv1, diagnostics = T)
a12_iv1_se<- coeftest(a12_iv1, vcov=vcovHC(a12_iv1, type="HC0"))

a12_iv2 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+(unemp*emig.share.t0))
a12_iv2_diag<- summary(a12_iv2, diagnostics = T)
a12_iv2_se<- coeftest(a12_iv2, vcov=vcovHC(a12_iv2, type="HC0"))


a12_iv3 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+(unemp*emig.share.t0))
a12_iv3_diag<- summary(a12_iv3, diagnostics = T)
a12_iv3_se<- coeftest(a12_iv3, vcov=vcovHC(a12_iv3, type="HC0"))

#Table A.12
stargazer::stargazer(a12_iv1_se, a12_iv2_se, a12_iv3_se, 
  omit = c('nuts_code2013', 'Year', 'Constant', 'voteshare.w1'), 
  omit.stat = c('ser', 'adj.rsq','rsq', 'f'), dep.var.labels.include = F, model.names = F,
  covariate.labels = c("Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Population"), add.lines = list(c("Observations", a12_iv1$n, a12_iv2$n, a12_iv3$n),
  c("NUTS3 FE","\\checkmark", "\\checkmark","\\checkmark"),
  c("Year FE","\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV","", "", "\\checkmark"), 
  c("First Stage F", round(a12_iv1_diag$diagnostics[1,3], 3), round(a12_iv2_diag$diagnostics[1,3],3), round(a12_iv3_diag$diagnostics[1,3],3))))

#######################################################################
#Table A.13. IV analysis with UK GDP growth rate (Poland)

a13_iv1 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), 
voteshare ~ emig.lag.1.voters+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+as.factor(nuts_code2013)+(gdp.growth.rate*emig.share.t1))
a13_iv1_diag<- summary(a13_iv1, diagnostics = T)
a13_iv1_se<- coeftest(a13_iv1, vcov=vcovHC(a13_iv1, type="HC0"))

a13_iv2 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), 
voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+as.factor(nuts_code2013)+(gdp.growth.rate*emig.share.t1))
a13_iv2_diag<- summary(a13_iv2, diagnostics = T)
a13_iv2_se<- coeftest(a13_iv2, vcov=vcovHC(a13_iv2, type="HC0"))

a13_iv3 <- AER::ivreg(data=pl_ro_sample %>% filter(cntry == "PL"), voteshare ~ emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+as.factor(Year) -1 |. - emig.lag.1.voters+imig.lag.1.voters+gdp.lag.1+unemp.percent.lag.1.n2+c_tf_r.lag.1+log(Pop)+voteshare.w1+as.factor(nuts_code2013)+(gdp.growth.rate*emig.share.t1))
a13_iv3_diag<- summary(a13_iv3, diagnostics = T)
a13_iv3_se<- coeftest(a13_iv3, vcov=vcovHC(a13_iv3, type="HC0"))


#Table A.13
stargazer::stargazer(a13_iv1_se, a13_iv2_se, a13_iv3_se, 
  omit = c('nuts_code2013', 'Year', 'Constant', 'voteshare.w1'), 
  omit.stat = c('ser', 'adj.rsq','rsq', 'f'), dep.var.labels.include = F, model.names = F,
  covariate.labels = c("Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Population"),add.lines = list(c("Observations", a13_iv1$n, a13_iv2$n, a13_iv3$n),c("NUTS3 FE","\\checkmark", "\\checkmark","\\checkmark"),c("Year FE","\\checkmark", "\\checkmark", "\\checkmark"), c("Lagged DV","", "", "\\checkmark"), c("First Stage F", round(a13_iv1_diag$diagnostics[1,3], 3), round(a13_iv2_diag$diagnostics[1,3],3), round(a13_iv3_diag$diagnostics[1,3],3))))


#######################################################################
#Table A.14. Regional Emigration and Individuals' Vote Choice (ESS)

ess_fin<- read.csv("ESS_emigration_clean.csv", stringsAsFactors = F)

a14_m1 <- glm(data = ess_fin, votefr2 ~ age+female+eisced+bigcity+unemp3m+emig.lag.1.pct+as.factor(cntry)+as.factor(ESSYear), family = "binomial")
a14_se1<- coeftest(a14_m1, vcov = vcovCL, cluster = ~nuts_code2013)

a14_m2 <- glm(data = ess_fin, votefr2 ~ age+female+eisced+unemp3m+bigcity+emig.lag.1.pct+imig.lag.1.pct+log(Pop)+gdp.lag.1+as.factor(ESSYear)+as.factor(cntry),family = "binomial")
a14_se2<- coeftest(a14_m2, vcov = vcovCL, cluster = ~nuts_code2013)

a14_m3<- glm(data = ess_fin, votefr2 ~ age+female+eisced+unemp3m+bigcity+emig.lag.1.pct+imig.lag.1.pct+log(Pop)+gdp.lag.1+unemp.percent.lag.1+as.factor(cntry)+as.factor(ESSYear),family = "binomial")
a14_se3<- coeftest(a14_m3, vcov = vcovCL, cluster = ~nuts_code2013)

a14_m4 <- glm(data = ess_fin, votefr2 ~ age+female+eisced+unemp3m+bigcity+emig.lag.1.pct+imig.lag.1.pct+log(Pop)+gdp.lag.1+unemp.percent.lag.1+c_tf_r.lag.1+as.factor(cntry)+as.factor(ESSYear),family = "binomial")
a14_se4<- coeftest(a14_m4, vcov = vcovCL, cluster = ~nuts_code2013)

#Table 14
stargazer::stargazer(a14_m1, a14_m2, a14_m3, a14_m4, 
  se=list(a14_se1[,2], a14_se2[,2], a14_se3[,2], a14_se4[,2]), 
  omit=c('cntry', 'ESSYear', 'Constant'), dep.var.caption = "Vote for Far-right Parties",
  add.lines = list(c("Country FE", "\\checkmark", '\\checkmark', '\\checkmark', '\\checkmark'),   
  c("Year FE", "\\checkmark", '\\checkmark', '\\checkmark', '\\checkmark')), 
  covariate.labels = c("Age", "Female", "Education", "Urban", "Unemployed", "Emigration", "Immigration", "Population", "GDP", "Unemployment", "Current Transfers"))


#######################################################################
#Table A.15. Regional Emigration and Individuals' Vote Choice (Random Effects)

polpan<- read.csv("polpan_clean.csv", stringsAsFactors = F)

polpan<- polpan %>% mutate(female = ifelse(GENDER == "Female", 1, 0))


#Table 6, model 1
a15_random1 <- plm(data= polpan, model = "random", index=c("ANONID", "Year"), 
  curvote.pis1 ~ AGE+female+BA_plus+nojob+emig.lag.1.percent, cluster='nuts_code2013')

#Table 6, model 2
a15_random2 <- plm(data= polpan, model = "random", index=c("ANONID", "Year"), 
  curvote.pis1 ~ AGE+female+BA_plus+nojob+emig.lag.1.percent+imig.lag.1.percent, cluster='nuts_code2013')

#Table 6, model 3
a15_random3 <- plm(data = polpan, model = "random", index=c("ANONID", "Year"), 
curvote.pis1 ~ AGE+female+BA_plus+nojob+emig.lag.1.percent+imig.lag.1.percent+gdp.lag.1.1000, cluster='nuts_code2013')


a15_random4 <- plm(data= polpan, model = "random", index=c("ANONID", "Year"), 
curvote.pis1 ~ AGE+female+BA_plus+nojob+emig.lag.1.percent+imig.lag.1.percent+gdp.lag.1.1000+unemp.percent.lag.1,  cluster = 'nuts_code2013')


a15_random5 <- plm(data= polpan, model = "random", index=c("ANONID", "Year"), 
  curvote.pis1 ~ AGE+female+BA_plus+nojob+emig.lag.1.percent+imig.lag.1.percent+gdp.lag.1.1000+unemp.percent.lag.1+c_tf_r+dis_inc.lag.1, cluster = 'nuts_code2013')


#Table A15
stargazer::stargazer(a15_random1, a15_random2, a15_random3, a15_random4, a15_random5, 
  omit=c("nuts_code2013", "Constant", 'Year'), omit.stat = c('rsq', 'adj.rsq', 'f'), 
  dep.var.caption = "Vote for Far-Right Parties", align = T, 
  covariate.labels = c("Age", "Female", "Education (BA+)", "Unemployed", "Emigration", "Immigration", "GDP", "Unemployment", "Current Transfers", "Disposable Income"))

